package com.example.vue_shop.components.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue_shop.components.shop.entity.Shop;
import com.example.vue_shop.components.shop.entity.ShopVo;
import com.example.vue_shop.components.shop.mapper.ShopMapper;
import com.example.vue_shop.components.shop.service.ShopService;
import com.example.vue_shop.components.shopparams.entity.ShopParams;
import com.example.vue_shop.components.shopparams.mapper.ShopParamsMapper;
import com.example.vue_shop.components.shoppriture.entity.ShopPicture;
import com.example.vue_shop.components.shoppriture.mapper.ShopPictureMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lipengwei
 */
@Service
public class ShopServiceImpl implements ShopService  {

    @Resource(name = "shopMapper")
    private ShopMapper shopMapper;

    @Resource(name = "shopPictureMapper")
    private ShopPictureMapper shopPictureMapper;

    @Resource(name = "shopParamsMapper")
    private ShopParamsMapper shopParamsMapper;

    @Override
    public Map<String, Object> getShopList(String query, int pageNum, int pageSize) {
        Page<Shop> queryPage = new Page<>();
        queryPage.setCurrent(pageNum);
        queryPage.setSize(pageSize);

        QueryWrapper<Shop> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(query)) {
           queryWrapper.like("shop_name", query);
        }
        Page<Shop> shopPage = shopMapper.selectPage(queryPage, queryWrapper);

        Map<String, Object> result = new HashMap<>(2);
        result.put("shopList", shopPage.getRecords());
        result.put("total", shopPage.getTotal());
        return result;
    }

    @Override
    public void delShop(int id) {
        shopMapper.deleteById(id);
    }

    @Override
    public void addShop(ShopVo shopVo) {

        System.err.println("==" + shopVo);
        // 商品，商品图片list新增 ，商品参数 更新
        Shop shop = new Shop();
        shop.setShopName(shopVo.getShopName());
        String shopPrice = shopVo.getShopPrice();
        shop.setShopPrice(shopPrice);
        // 把shopPrice 按小数点分割
        String[] shopPriceArr = shopPrice.split("\\.");
        shop.setShopPriceInt(Integer.parseInt(shopPriceArr[0]));
        shop.setShopPriceDouble( shopPriceArr[1] == null ? 0 :
                Integer.parseInt(shopPriceArr[1]));
        shop.setShopNumber(shopVo.getShopNumber());
        shop.setShopWeight(shopVo.getShopWeight());
        shop.setShopType(shopVo.getShopType());
        shop.setShopIntroduce(shopVo.getShopIntroduce());

        int insertId = shopMapper.insert(shop);

        // 添加图片
        List<ShopPicture> shopPictures = shopVo.getShopPicture();
        shopPictures.forEach(item -> {
            item.setShopId(insertId);
            shopPictureMapper.insert(item);
        });

        // 添加参数, 更新参数
        List<ShopParams> shopParams = shopVo.getShopParams();
        shopParams.forEach(item -> shopParamsMapper.updateById(item));


    }
}
